package mx.com.matersys.vacs.web;

import mx.com.matersys.vacs.domain.Especialista;
import mx.com.matersys.vacs.domain.Paciente;
import mx.com.matersys.vacs.domain.UserVacs;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import flexjson.JSONSerializer;

@RooWebScaffold(path = "uservacses", formBackingObject = UserVacs.class)
@RequestMapping("/uservacses")
@Controller
public class UserVacsController {
	

	  @RequestMapping(value = "/consultaUsuario", method = RequestMethod.GET, headers = "Accept=application/json")
	    @ResponseBody
	    public ResponseEntity<java.lang.String> getUsuario() {
	        HttpHeaders headers = new HttpHeaders();
	        
	        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	        String username=null;
	        if (principal instanceof UserDetails) {
	            username = ((UserDetails)principal).getUsername();
	        } else {
	            username = principal.toString();
	        }
	        UserVacs user =null;
	        if(username!=null){
	        	 user = UserVacs.findUserVacs(username);
	        }
	        Object usuario=null;
	        if(user !=null ){
	        	if(user.getIsespecialista()==1){
	        		usuario=Especialista.findEspecialista(user.getIdusuario());
	        		
	        	}else {
	        		usuario = Paciente.findPaciente(user.getIdusuario());
	        	}
	        }   
	        
	        System.out.println("PacienteController::especialista---------"+username);
	        
	        String json = new JSONSerializer().exclude("*.class").serialize(usuario);
	        return new ResponseEntity<String>(json, headers, HttpStatus.OK);
	    }
	
}
